//
//  AbnormalViewController.m
//  安好
//
//  Created by 冯凯 on 14-11-26.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "AbnormalViewController.h"
#import "OpinionDetailTableViewCell.h"
@interface AbnormalViewController ()

@end

@implementation AbnormalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#f2f2f2"];
    [self makeNavBar];
    [self loadData];
    [self createTableView];
}
#pragma mark - 配置导航条
-(void)makeNavBar
{
    self.navigationItem.title = @"异常退出";
    // 返回按钮图片
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backMoreVC)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem= backBtnImg;
    // 完成
    UIBarButtonItem *OkBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_完成.png"] style:UIBarButtonItemStylePlain target:self action:@selector(finish)];
    OkBtnImg.tintColor = [UIColor whiteColor];
    OkBtnImg.tag = 100;
    self.navigationItem.rightBarButtonItem = OkBtnImg;
    
}
-(void)createTableView
{
    self.systemTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0 , kScreenWidth, kScreenHeight - iOS7TopMargin) style:UITableViewStyleGrouped];
    self.systemTableView.delegate = self;
    self.systemTableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.systemTableView.showsHorizontalScrollIndicator = NO;
    self.systemTableView.showsVerticalScrollIndicator = NO;
    self.systemTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    [self.view addSubview:self.systemTableView];
    self.opinionTextView = [[CBTextView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 180)];
    self.opinionTextView.placeHolder = @"如果上述没有你所遇到的问题，请在这里补充";
    self.opinionTextView.placeHolderFont = [UIFont systemFontOfSize:13];
    self.opinionTextView.textView.delegate = self;
    self.systemTableView.tableFooterView = self.opinionTextView;
}

- (void)backMoreVC
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)finish
{
    [self loadWithData];
}

-(void)loadData
{
    self.setArray = @[@"输入文字时",@"咨询医生问题时",@"浏览帖子时",@"上传图片时",@"启用拍照发送图片时",@"发送语音时",@"点击体检室时"];
}
- (void)loadWithData
{
    self.allString = [[NSMutableString alloc] init];

    if ([self.inputString length] != 0) {
        [self.allString appendFormat:@",%@",self.inputString];
    }
    if ([self.consuString length] != 0){
        [self.allString appendFormat:@",%@",self.consuString];
    }
    if ([self.browseString length] != 0){
        [self.allString appendFormat:@",%@",self.browseString];
    }
    if ([self.uploadString length] != 0){
        [self.allString appendFormat:@",%@",self.uploadString];
    }
    if ([self.enableString length] != 0){
        [self.allString appendFormat:@",%@",self.enableString];
    }
    if ([self.sendString length] != 0){
        [self.allString appendFormat:@",%@",self.sendString];
    }
    if ([self.clickString length] != 0){
        [self.allString appendFormat:@",%@",self.clickString];
    }
    [self.opinionTextView.textView resignFirstResponder];
    if ([self.allString length] >=1) {
        self.allIDString = [self.allString substringFromIndex:1];
    }else {
        self.allIDString = @"";
    }
    if ([self.opinionTextView.textView.text length] == 0) {
        self.opinionTextView.textView.text = @"";
    }
    //postUrl
    NSString *postURL = [NSString stringWithFormat:@"%@user/newfeedback/",kBaseURL];
    //post参数列表
        NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion,@"type":@"1",@"catgoryid":@"1",@"additional":self.opinionTextView.textView.text,@"description":self.allIDString};
    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //开始请求
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        //用NSNumberFormatter把 NSNumber类型的数字-->转成字符串
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];//得到字符串对象: "0" / "1"
        if ([statusString isEqualToString:@"0"]) {
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
//                                                            message:[responseObject objectForKey:@"msg"]
//                                                           delegate:self
//                                                  cancelButtonTitle:@"确定"
//                                                  otherButtonTitles:nil,nil];
//            alert.tag = 100;
//            [alert show];
            [self showCustomHudSingleLine:[responseObject objectForKey:@"msg"]];
        }else{
            
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
//                                                            message:@"您的意见已成功提交，感谢你对安好的关注！"
//                                                           delegate:self
//                                                  cancelButtonTitle:@"确定"
//                                                  otherButtonTitles:nil,nil];
//            alert.tag = 101;
//            [alert show];
            [self showCustomHudSingleLine:@"您的意见已成功提交，感谢你对安好的关注"];
            [self performSelector:@selector(popView) withObject:nil afterDelay:1.5f];

            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("失败");
    }];
}
- (void)popView
{
    [self.navigationController popViewControllerAnimated:YES];

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if (alertView.tag == 101) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.setArray.count;
}
//设置段头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 46;

}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (kScreenWidth == 414){
        return 56;
    }else{
        return 44;
    }
}
#pragma mark ----2中cell的加载
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    OpinionDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OpinionDetailTableViewCell"];
    if (cell == nil) {
        if (kScreenWidth == 414){
            cell = [[[NSBundle mainBundle] loadNibNamed:@"OpinionDetailTableViewCell" owner:self options:nil] lastObject];
            
        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"OpinionDetailTableViewCell" owner:self options:nil] firstObject];

        }
    }
    cell.detailLabel.text = self.setArray[indexPath.row];
    cell.selectButton.tag = indexPath.row;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell.selectButton addTarget:self action:@selector(selectView:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 46)];
    view.backgroundColor = [UIColor clearColor];
    UILabel * sectionLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 13, kScreenWidth, 20)];
    if (kScreenWidth == 414) {
        sectionLabel.frame = CGRectMake(15, 11, kScreenWidth, 20);
        sectionLabel.font = [UIFont systemFontOfSize:16];

    }else{
        sectionLabel.font = [UIFont systemFontOfSize:14];
        
    }
    sectionLabel.text = @"出现异常退出的操作或者场景是?";
    [view addSubview:sectionLabel];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    view.backgroundColor = [UIColor clearColor];
    
    return view;
}
int lastSelect;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OpinionDetailTableViewCell* cell = (OpinionDetailTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    if (cell.selectButton.tag <= 9){
        [cell.selectButton setImage:[UIImage imageNamed:@"登录_选中"] forState:UIControlStateNormal];
        if (cell.selectButton.tag == 0) {
            self.inputString = self.setArray[indexPath.row];
        }else if (cell.selectButton.tag == 1){
            self.consuString = self.setArray[indexPath.row];
        }else if (cell.selectButton.tag == 2){
            self.browseString = self.setArray[indexPath.row];
        }else if (cell.selectButton.tag == 3){
            self.uploadString = self.setArray[indexPath.row];
        }else if (cell.selectButton.tag == 4){
            self.enableString = self.setArray[indexPath.row];
        }else if (cell.selectButton.tag == 5){
            self.sendString = self.setArray[indexPath.row];
        }else if (cell.selectButton.tag == 6){
            self.clickString = self.setArray[indexPath.row];
        }
        cell.selectButton.tag = 10 + indexPath.row;
    }else{
        [cell.selectButton setImage:[UIImage imageNamed:@"登录_未选中"] forState:UIControlStateNormal];
        if (cell.selectButton.tag == 10) {
            self.inputString = @"";
        }else if (cell.selectButton.tag == 11){
            self.consuString = @"";
        }else if (cell.selectButton.tag == 12){
            self.browseString = @"";
        }else if (cell.selectButton.tag == 13){
            self.uploadString = @"";
        }else if (cell.selectButton.tag == 14){
            self.enableString = @"";
        }else if (cell.selectButton.tag == 15){
            self.sendString = @"";
        }else if (cell.selectButton.tag == 16){
            self.clickString = @"";
        }
        cell.selectButton.tag =  indexPath.row;

    }
}
- (void)selectView:(UIButton *)button
{
    if (button.tag == 0) {
        self.inputString = self.setArray[0];
        [button setImage:[UIImage imageNamed:@"登录_选中"] forState:UIControlStateNormal];
        button.tag = 10;
    }else if (button.tag == 1){
        self.consuString = self.setArray[1];
        button.tag = 11;
        [button setImage:[UIImage imageNamed:@"登录_选中"] forState:UIControlStateNormal];
    }else if (button.tag == 2){
        self.browseString = self.setArray[2];
        button.tag = 12;
        [button setImage:[UIImage imageNamed:@"登录_选中"] forState:UIControlStateNormal];
    }else if (button.tag == 3){
        self.uploadString = self.setArray[3];
        button.tag = 13;
        [button setImage:[UIImage imageNamed:@"登录_选中"] forState:UIControlStateNormal];
    }else if (button.tag == 4){
        self.enableString = self.setArray[4];
        button.tag = 14;
        [button setImage:[UIImage imageNamed:@"登录_选中"] forState:UIControlStateNormal];
    }else if (button.tag == 5){
        self.sendString = self.setArray[5];
        button.tag = 15;
        [button setImage:[UIImage imageNamed:@"登录_选中"] forState:UIControlStateNormal];
    }else if (button.tag == 6){
        self.clickString = self.setArray[6];
        button.tag = 16;
        [button setImage:[UIImage imageNamed:@"登录_选中"] forState:UIControlStateNormal];
    }
     else if (button.tag == 10) {
        self.inputString = @"";
        button.tag = 0;
        [button setImage:[UIImage imageNamed:@"登录_未选中"] forState:UIControlStateNormal];
    }else if (button.tag == 11){
        self.consuString = @"";
        button.tag = 1;
        [button setImage:[UIImage imageNamed:@"登录_未选中"] forState:UIControlStateNormal];
    }else if (button.tag == 12){
        self.browseString = @"";
        button.tag = 2;
        [button setImage:[UIImage imageNamed:@"登录_未选中"] forState:UIControlStateNormal];
    }else if (button.tag == 13){
        self.uploadString = @"";
        button.tag = 3;
        [button setImage:[UIImage imageNamed:@"登录_未选中"] forState:UIControlStateNormal];
    }else if (button.tag == 14){
        self.enableString = @"";
        button.tag = 4;
        [button setImage:[UIImage imageNamed:@"登录_未选中"] forState:UIControlStateNormal];
    }else if (button.tag == 15){
        self.sendString = @"";
        button.tag = 5;
        [button setImage:[UIImage imageNamed:@"登录_未选中"] forState:UIControlStateNormal];
    }else if (button.tag == 16){
        self.clickString = @"";
        button.tag = 6;
        [button setImage:[UIImage imageNamed:@"登录_未选中"] forState:UIControlStateNormal];
    }

}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [UIView animateWithDuration:0.3 animations:^
     {
         if ([self.opinionTextView.textView.text isEqualToString:@"如果上述没有你所遇到的问题，请在这里补充"]) {
             self.opinionTextView.placeHolder = @"";
         }
         self.opinionTextView.textView.text = self.opinionTextView.textView.text;
         self.systemTableView.frame = CGRectMake(0, -353, kScreenWidth, kScreenHeight+100);
     } completion:^(BOOL finished)
     {
     }];
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    return YES;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if ([text isEqualToString:@"\n"]) {
        [UIView animateWithDuration:0.3 animations:^
         {
             if ([self.opinionTextView.textView.text length] == 0) {
                 self.opinionTextView.placeHolder = @"如果上述没有你所遇到的问题，请在这里补充";
             }
             self.systemTableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - iOS7TopMargin);
         } completion:^(BOOL finished)
         {
         }];
        [self.opinionTextView.textView resignFirstResponder];
        return NO;
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
